import {
  comments
} from "../../../../js/modules/mall.js";
import {
  storage,
  util,
  dialog,
  api
} from '../../../../js/util';
import {
  router
} from '../../../../js/router';
import {
  upload
} from '../../../../js/modules/material.js'
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"commentDetail",

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    textLength: 200,
    isLoadComplete:false,
    orderInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var oddNumber = options.oddNumber;
    if (oddNumber == "") {
      util.tips("订单号不能为空");
      var params = {
        path: "orderList",
        params: {}
      }
      router.navigateBack(params);
      return;
    }
    this.data.oddNumber=oddNumber;
    this.getWaitCommentOrderInfo();

  },
  getWaitCommentOrderInfo(){
    var that = this;
    comments.getWaitCommentOrderInfo(this.data.oddNumber, function(res) {
      if (res.Success) {
        that.setData({
          isLoadComplete:true,
          orderInfo: res.Content
        });
      }else{
        util.tips(res.Message);
      }
    });
  },
  /**
   * 文体框输入事件
   */
  onInputCommentsEvent: function(e) {
    var idx = e.currentTarget.dataset.idx;
    var text = e.detail.value;
    this.data.orderInfo.Items[idx].Content = text;
    this.data.orderInfo.Items[idx].Short = this.data.textLength - text.length;
    this.setData({
      orderInfo: this.data.orderInfo
    });
  },
  /**
   * 删除
   */
  onDelImageEvent: function(e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    var imgidx = e.currentTarget.dataset.imgidx;
    var image_list = that.data.orderInfo.Items[idx].ImageList
    image_list.splice(imgidx, 1);
    var virtual_list = that.data.orderInfo.Items[idx].VirtualPaths;
    virtual_list.splice(imgidx,1);
    that.data.orderInfo.Items[idx].ImageList = image_list;
    that.data.orderInfo.Items[idx].VirtualPaths = virtual_list;
    that.setData({
      orderInfo: that.data.orderInfo
    })
  },
  /**
   * 上传
   */
  bindUploadEvent: function(e) {
    var idx = e.currentTarget.dataset.idx;
    var that = this,image_list = [];
    var current = that.data.orderInfo.Items[idx];
    current.ImageList = current.ImageList||[];
    if (current.ImageList.length>=6){
      util.tips("最多只能上传6张");
      return;
    }
    var imgcount = 6 - current.ImageList.length;
    current.VirtualPaths = current.VirtualPaths || [];
    wx.chooseImage({
      count: imgcount,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths;
        tempFilePaths.map(function(item){
          current.ImageList.push(item);
        })
        that.data.orderInfo.Items[idx]=current;
        that.setData({
          orderInfo: that.data.orderInfo
        });
        var data = {num:1010,type:1,tempFilePaths:tempFilePaths};
        upload.upload_image_server(data, function(image_list) {
          if (image_list.length==0){
            util.alert("上传失败");
            return;
          }
          image_list.map(function (item) {
            current.VirtualPaths.push(item);
          });
          that.data.orderInfo.Items[idx] = current;
        })
      }
    });
  },
  /**
   * 星星数量改变事件
   */
  onStartNumEvent: function(e) {
    e = e.detail;
    var idx = e.key;
    this.data.orderInfo.Items[idx].Score = e.score;
  },
  /**
   * 星星数量改变事件
   */
  onOtherCommentEvent: function(e) {
    e = e.detail;
    this.data.orderInfo[e.key] = e.score;
  },
  /**
   * 匿名事件
   */
  onAnonymousEvent: function(e) {
    this.data.orderInfo.IsAnonymous = !this.data.orderInfo.IsAnonymous;
  },
  /**
   * 立即评论
   */
  onCommentEvent: function() {
    var that=this;
    var url = that.getPreUrl();
    comments.submitComments(this.data.orderInfo, function(res) {
      if (res.Success) {
        var tip ="评价成功！";
        if (res.Message.length>0){
          tip = "评价成功，" + res.Message;
        }
        wx.showModal({
          title: '提示',
          content: tip,
          showCancel: false,
          success:function(res){
            if(res.confirm){
              router.navigateBack({},true);
            }
          }
        })

      } else {
        util.tips("评价失败" + res.Message)
      }
    });
  },
  getPreUrl: function () {
    const pages = getCurrentPages();
    const length = pages.length;
    const prePage = pages[length - 2]; //上一页
    return prePage.route //url
  },
    /**
   * 刷新页面
   */
  refreshPage(){
    this.getWaitCommentOrderInfo();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})